package com.microservice.system.service.impl;

import com.microservice.core.domain.AjaxResult;
import com.microservice.core.utils.BCryptUtil;
import com.microservice.model.system.Permission;
import com.microservice.model.system.Role;
import com.microservice.model.system.User;
import com.microservice.system.data.UserDataService;
import com.microservice.system.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserDataService userService;

    @Override
    public AjaxResult add(User user) {
        user.setPassword(BCryptUtil.encrypt(user.getPassword()));
        boolean add = userService.batchAddUserByUtil(Collections.singletonList(user));
        if (!add) {
            return AjaxResult.error("添加用户失败");
        }
        return AjaxResult.success("新增用户成功");
    }

    @Override
    public AjaxResult getPermissionByUsername(String username) {
        List<Permission> list = userService.getPermissionByUsername(username);
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getRoleByUsername(String username) {
        List<Role> list = userService.getRoleByUsername(username);
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getUserByUsername(String username) {
        return AjaxResult.success(userService.getUserByUsername(username));
    }

    @Override
    public AjaxResult getUserList() {
        return AjaxResult.success(userService.getUserList());
    }

    @Override
    public AjaxResult delete(String id) {
        User user = new User();
        user.setId(id);
        boolean removeById = userService.removeById(user);
        if (!removeById){
            return AjaxResult.error("删除用户失败");
        }
        return AjaxResult.success("删除用户成功");
    }
}
